<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 4/24/17
 * Time: 4:01 PM
 */

namespace common\models;

/**
 * Class ApiRole
 * @package common\models
 *
 * @property array $apiRouteArr
 * @property ApiRoleRoute[] $apiRoleRoutes
 * @property ApiRoute[] $apiRoutes
 * @property ApiRoleUser[] $apiRoleUsers
 * @property User[] $users
 */
class ApiRole extends \common\models\base\ApiRole
{
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '角色名',
            'created_at' => \Yii::t('app', 'Created At'),
            'updated_at' => \Yii::t('app', 'Updated At'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoleRoutes()
    {
        return $this->hasMany(ApiRoleRoute::className(), ['api_role_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoutes()
    {
        return $this->hasMany(ApiRoute::className(), ['id' => 'api_route_id'])->viaTable('{{%api_role_route}}', ['api_role_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoleUsers()
    {
        return $this->hasMany(ApiRoleUser::className(), ['api_role_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id' => 'user_id'])->viaTable('{{%api_role_user}}', ['api_role_id' => 'id']);
    }

    /**
     * @return array
     */
	public function getApiRouteArr()
    {
        $route = [];
        $x = ApiRoleRoute::find()->where(['api_role_id'=>$this->id])->all();
        foreach ($x as $k => $v){
            $routes[] = $v->apiRoute->route;
        }
        return $route;
    }
}